<template>
  <div class="api-manage">
    <div class="content-box">
      <div class="content-header">服务接口管理</div>
      <div class="content-body" v-if="!isDetailShow">
        <div class="inter-box">
          <div
            class="inter-item"
            @click="handleOpenDetail(item, index)"
            v-for="(item, index) in state.api_models"
            :key="index"
          >
            <div class="title">{{ item.proName || '--' }}</div>
            <div class="operator">
              <div class="left">
                <el-icon color="#333" @click.stop="handleAddProject(item)">
                  <Edit />
                </el-icon>
                <el-icon color="#333" @click.stop="handleDeleteProject(item, index)">
                  <Delete />
                </el-icon>
              </div>
              <div class="right">
                <span class="num">{{ item.apiNum || 0 }}</span>
                <span>个接口</span>
              </div>
            </div>
          </div>
          <div class="add-item" @click="handleAddProject()">
            <div class="icon">
              <el-icon>
                <CirclePlus />
              </el-icon>
            </div>
            <div class="title">添加项目</div>
          </div>
        </div>
      </div>
      <!-- 详情 -->
      <div class="content-body" v-if="isDetailShow">
        <api-details :apiModels="state.proObj" @closeDiglog="handleCloseDiglog" />
      </div>
    </div>
    <!-- //添加项目弹框 -->
    <AddProject
      :projectObj="state.projectObj"
      :showDialog="showDialog"
      @saveFormData="handleSaveProject"
      @colseLog="handleCloseDialog"
    />
  </div>
</template>
<script setup>
import ApiDetails from './cpns/ApiDetails.vue'
import AddProject from './cpns/addProject.vue'
import {
  getProjectList,
  addProject,
  deleteProject,
  modifyProject
} from '@/services/modules/external-service'
import { ElMessage, ElMessageBox } from 'element-plus'
// import api_models from '@/assets/data/api_interface/api_models.json'

import { ref, reactive, onMounted } from 'vue'
const state = reactive({
  api_models: [],
  proObj: {
    apiNum: '',
    proName: ''
  },
  projectObj: {}
})
const showDialog = ref(false)
// 接口详情
const isDetailShow = ref(false)
//
onMounted(() => {
  getApiModels()
})
//查询项目接口内容
function getApiModels() {
  getProjectList().then((res) => {
    if (res.code === 200) {
      state.api_models = res.data
    }
  })
}
//保存项目
function handleSaveProject(data) {
  if (data.id) {
    modifyProject(data).then((res) => {
      if (res.code == 200) {
        ElMessage({
          type: 'success',
          message: '修改成功'
        })
        getApiModels()
      }
    })
  } else {
    addProject(data).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg)
        getApiModels()
      }
    })
  }
}
//删除项目
function handleDeleteProject(item, index) {
  ElMessageBox.confirm(`是否删除${item.proName}`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      deleteProject({ id: item.id }).then((res) => {
        if (res.code === 200) {
          ElMessage.success(res.msg)
          getApiModels()
        }
      })
    })
    .catch(() => {})
}
//关闭
function handleCloseDialog(value) {
  showDialog.value = value
}
//添加项目
function handleAddProject(item) {
  state.projectObj = item
  showDialog.value = true
}
const handleOpenDetail = (item, index) => {
  state.proObj.proName = item.proName
  state.proObj.apiNum = item.apiNum
  state.proObj.id = item.id
  isDetailShow.value = true
}

const handleCloseDiglog = () => {
  isDetailShow.value = false
  getApiModels()
}
</script>
<style lang="less" scoped>
.api-manage {
  height: 100%;

  .content-box {
    height: 100%;

    .content-header {
      display: flex;
      align-items: center;
      height: 50px;
      padding-left: 20px;
      border-bottom: 1px solid var(--border-primary);
      font-size: var(--header-fontsize);
      font-weight: 700;
      box-sizing: border-box;
    }

    .content-body {
      height: calc(100% - 50px);

      .inter-box {
        display: flex;
        flex-wrap: wrap;
        padding: 10px;
        box-sizing: border-box;

        .inter-item {
          display: flex;
          flex-direction: column;
          width: 300px;
          height: 105px;
          cursor: pointer;
          background: linear-gradient(
            178.08deg,
            rgba(217, 244, 255, 1) 0%,
            rgb(255, 255, 255) 100%,
            rgba(255, 255, 255, 1) 100%
          );
          border: 1px solid rgba(0, 0, 0, 0.1);
          box-shadow: 0px 1px 6px 0px rgba(0, 0, 0, 0.06);

          border-radius: 10px;
          margin-right: 15px;
          margin-bottom: 10px;

          .title {
            flex: 1;
            margin: 15px 0 0 15px;
            font-size: 16px;
          }

          .operator {
            display: flex;
            justify-content: space-between;
            align-items: center;

            padding: 0 10px;
            height: 30px;

            .left {
              i:nth-of-type(1) {
                margin-right: 15px;
              }
            }

            .right {
              .num {
                color: #00f;
                margin-right: 5px;
                font-weight: 700;
              }
            }
          }

          &:hover {
            box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
          }
        }
        .add-item {
          display: flex;
          justify-content: center;
          align-items: center;
          // flex-direction: column;
          width: 215px;
          height: 105px;
          cursor: pointer;
          background: linear-gradient(
            178.08deg,
            rgba(217, 244, 255, 1) 0%,
            rgb(255, 255, 255) 100%,
            rgba(255, 255, 255, 1) 100%
          );
          border: 1px solid rgba(0, 0, 0, 0.1);
          box-shadow: 0px 1px 6px 0px rgba(0, 0, 0, 0.06);

          border-radius: 5px;
          margin-right: 15px;
          .icon {
            font-size: 25px;
            color: var(--border-primary);
            display: flex;
            justify-content: center;
          }
          .title {
            font-size: 20px;
            color: var(--border-primary);
            margin-left: 10px;
          }

          &:hover {
            box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
          }
        }
      }
    }
  }
}
</style>
